<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserRecharge extends Model
{
    protected $pk = "recharge_id";

    /**
     * 返回支付方式
     * @return array
     */
    function backPayTypeList(){
        return [
            'wechat'=>'微信支付',
            'wechatApple'=>'微信小程序支付',
            'wechatH5'=>'微信支付',
            'aliPay'=>'支付宝支付',
        ];
    }

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where){
        return $this->where($where)->find();
    }

    /**
     * 获取信息
     * @param $id
     * @param $addWhere
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id,$addWhere = [],$field = '*'){
        $where = ['id'=>$id];
        return $this->where($where)->where($addWhere)->field($field)->find();
    }

    /**
     * 获取指定用户列表
     * @param $userId
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getUserList($userId,$page=1,$limit=20,$field = '*'){
        $where = ['uid'=>$userId,'paid'=>1];
        $count = $this->where($where)->count();
        $list = $this->where($where)->page($page,$limit)->field($field)->order('create_time','desc')
            ->select()->toArray();
        $payTypeList = $this->backPayTypeList();
        foreach ($list as $key=>$value){
            $list[$key]['type_name'] = $payTypeList[$value['recharge_type']];
        }
        return compact('count','list');
    }

    /**
     * 添加记录
     * @param $userId
     * @param $addData
     * @return int|string
     */
    function addRecord($userId,$addData){
        $addData = [
            'uid'=>$userId,
            'order_id'=>$this->getOrderNumber(),
            'price'=>$addData['price'],
            'give_price'=>0,
            'recharge_type'=>$addData['recharge_type'],
            'paid'=>0,
            'create_time'=>date("Y-m-d H:i:s"),
            'refund_price'=>0,
        ];
        $result = $this->insertGetId($addData);
        if(!$result) return false;
        $addData['recharge_id'] = $result;
        return $addData;
    }

    /**
     * 获取订单编号
     * @return string
     */
    function getOrderNumber(){
        list($msec, $sec) = explode(' ', microtime());
        $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
        $rand1 = mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
        $rand2 = mt_rand(10000, 99999);
        $orderSn = 're' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369)).$rand1.$rand2;
        if(strlen($orderSn) > 32){
            $orderSn = substr($orderSn,0,32);
        }else if(strlen($orderSn) < 32){
            $orderSn = str_pad($orderSn,32,'0',STR_PAD_RIGHT);
        }
        return $orderSn;
    }

}
